#include "infos.h"


int onModulePin = 2;
int tmpin = 5;

char inchar;
char receiver[10];

void switchModule(){
  digitalWrite(onModulePin,HIGH);
  delay(2000);
  digitalWrite(onModulePin,LOW);  
  delay(2000);
}

void setup(){

  pinMode(onModulePin, OUTPUT);

  Serial.begin(19200);
  switchModule();

  for (int i=0;i<2;i++){            //On attends que le module gsm s'allume...
    delay(5000);                        
  } 

  Serial.print("AT+CPIN=");
  Serial.print(34,BYTE);
  Serial.print(pin_code);
  Serial.println(34,BYTE);
  delay(2000);

  Serial.println("AT+CMGF=1");         // passage en mode texte pour les SMS
  Serial.println("AT+CNMI=2,2,0,0");   // redirection des SMS dans le port serie
}

void parse_receiver(){
  inchar = Serial.read();
  delay(10);
  if (inchar == '+')
  {
    delay(10);
    inchar = Serial.read();
    if (inchar=='3') 
    {
      delay(10);
      inchar = Serial.read();
      if (inchar=='3') //Si on recoit la chaine "+33" (debut du numero)
      {
        delay(10);
        receiver[0] = '0';
        int i;
        for(i = 1; i < 10; i++){
          inchar = Serial.read();
          receiver[i] = inchar;
        }
      }
    }

  }
}

void loop(){
  //If a character comes in from the cellular module...
  if(Serial.available() > 0)
  {

    parse_receiver();

    inchar = Serial.read();
    delay(10);
    if (inchar == '#')
    {
      delay(10);
      inchar = Serial.read();
      if (inchar=='4') //Si on recoit la chaine "#4"
      {
        delay(10);
        send_sms((getVoltage(tmpin) - 0.5) * 100);
      }
    }
  }
  delay(10);
}


void send_sms(int lumi){
  delay(1500);
  Serial.print("AT+CMGS=");
  Serial.print(34,BYTE);
  delay(1500); 
  int i;
  for(i = 0; i < 10; i++)
    Serial.print(receiver[i]);
  Serial.println(34,BYTE);
  delay(10);
  Serial.print("LUM ");
  delay(10);
  Serial.print(lumi,DEC);
  delay(10);
  Serial.print('%');
  delay(500);
  Serial.print(0x1A,BYTE);
  delay(1500);
}

void getLumi(int pin){
  lumi = analogRead(pin);
  lumi = map(lumi, 0, 900, 100, -15);
}






